//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_dismissbuttonaction.sqf"
#include "x_setup.sqf"

#include "x_uiinc.sqf"

__TRACE("dismissbuttonaction");
if (__pGetVar(GVAR(recdbusy))) exitWith {};
__pSetVar [QGVAR(recdbusy), true];
__TRACE("dismissbuttonaction2");
private ["_control", "_idx", "_unit", "_ctrl", "_control2"];
_control = __uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1001;
_idx = lbCurSel _control;
__TRACE_1("",_idx);
if (_idx == -1) exitWith {
	__pSetVar [QGVAR(recdbusy), false];
};

__DEC(GVAR(current_ai_num));
__TRACE_1("",GVAR(current_ai_num));

_control lbDelete _idx;

_unit = GVAR(current_ai_units) select _idx;
GVAR(current_ai_units) set [_idx, -1];
GVAR(current_ai_units) = GVAR(current_ai_units) - [-1];

if (!isPlayer _unit) then {
	if (vehicle _unit == _unit) then {
		deleteVehicle _unit;
	} else {
		moveOut _unit;
		[_unit] spawn {
			scriptName "spawn_d_fnc_dismissbuttonaction_waitvec";
			private ["_unit"];
			PARAMS_1(_unit);
			waitUntil {sleep 0.212;vehicle _unit == _unit};
			deleteVehicle _unit;
		};
	};
};

_ctrl = __uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1010;
if (!ctrlShown _ctrl) then {
	_ctrl ctrlShow true;
};

if (GVAR(current_ai_num) == 0) then {
	(__uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1011) ctrlShow false;
	(__uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1012) ctrlShow false;
};

_control2 = __uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1030;
_control2 ctrlSetText format [(localize "STR_DOM_MISSIONSTRING_693"), GVAR(current_ai_num), GVAR(max_ai)];
__pSetVar [QGVAR(recdbusy), false];